package com.software.form.taskform.dao;

import java.util.List;

import com.software.common.CommonUtils;

public class TaskFormTransaction {

	private TaskFormDao taskFormDao;

	public TaskFormDao getTaskFormDao() {
		return taskFormDao;
	}

	public void setTaskFormDao(TaskFormDao taskFormDao) {
		this.taskFormDao = taskFormDao;
	}
	
	public TaskForm queryNow(String name){
		TaskForm task = new TaskForm();
		task.setDriverName(name);
		TaskForm tf = taskFormDao.queryTaskCurrent(task);
		if(tf != null)
			return tf;
		return null;
	}
	
	public List<TaskForm> queryByName(String name){
		TaskForm task = new TaskForm();
		task.setDriverName(name);
		return taskFormDao.queryTaskByName(task);
	}
	
	public List<TaskForm> queryByNo(String no){
		TaskForm task = new TaskForm();
		task.setNo(no);
		return taskFormDao.queryTaskByNo(task);
	}
	
	public boolean addTask(String no, String startTime,String dueTime,
			String comment, String driverName){
		TaskForm task = new TaskForm();
		task.setNo(no);
		task.setStartTime(startTime);
		task.setDueTime(dueTime);
		task.setDriverName(driverName);
		task.setComment(comment);
		
		return taskFormDao.addTask(task);
	}
	
	public String updateStatus(int id,String status){
		TaskForm tf = taskFormDao.queryById(id);
		String end = tf.getEndTime();
		if(end != null)
			return tf.getStatus();
		else{
			status = CommonUtils.getNowTime()+" arrived " + status+"/";
			String s = null;
			if(tf.getStatus() == null)
				s =  status;
			else
				s = tf.getStatus() + status;
			taskFormDao.updateTaskStatus(s,id);
			return s;
		}
	}
	
	public String updateEndTime(int id,String endTime){
		TaskForm tf = taskFormDao.queryById(id);
		String status = tf.getStatus();
		String end = tf.getEndTime();
		if(end != null){
			return null;
		}else{
			if(taskFormDao.updateTaskEndTime(endTime, id)){
				String str = status+"/"+endTime+" finished the task";
				taskFormDao.updateTaskStatus(str,id);
				return str;
			}else{
				return null;
			}
		}
	}
	
	public TaskForm getTaskFormById(int id){
		return taskFormDao.queryById(id);
	}
}
